A good answer might be:

The parameter x holds a reference to the object that main() created.


Call by Value

Here is a picture that shows what the executing program looks like just as the print() method starts running. Remember that Java uses call by value to pass data into methods. With array parameters, this means that the parameter holds the value of the reference to the array.

There is only one array object, the one created by main(). When the print() method is running it has a reference to this array in its parameter x. Here are some details:

  1. ArrayDemo is shown in dotted lines because it is a class definition, not an object.
  2. ArrayDemo's static method main() is running.
  3. The variable ar1 refers to the array object.
  4. The variable operate refers to an ArrayOps object.
  5. The picture shows what happens when the print() method of operate is called with ar1 as a parameter.
  6. Just after the call, the formal parameter x refers to the same object as does the variable ar1 .

Now the print() method runs, using the contents of x to refer to the array it works with. While it is running the method works with x just like the code in the previous chapters. When it finishes running, control returns to main().

Usually you do not think about things in such excruciating detail. You would think "call a method to print the array" and would write

operate.print( ar1 ); 

But sometimes you really need to know what is going on. Please look the picture over and read over the details a few times. If you rush through this material, you risk missing concepts that are necessary for understanding future topics.

QUESTION 4:

(Test of Understanding: ) Could the main() method create a second array and use the print() method with it?